﻿Imports System.Net.Http
Imports System.Net
Imports System.Text
Imports LSW.Security
Imports Windows.Networking.PushNotifications
Imports LSW.Exceptions
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Json

Namespace Net
    Public Module HttpHelper
        Public Async Function GetHtml(url As String) As Task(Of String)
            Dim h As New HttpClient
            Dim req = Await h.GetAsync(New Uri(url, UriKind.RelativeOrAbsolute))
            Dim txt = Await req.Content.ReadAsStringAsync
            Return txt
        End Function

        Public Async Function SendPushServerUrl(url As String) As Task(Of String)
            Dim channel As PushNotificationChannel = Nothing
            Try
                channel = Await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync
            Catch ex As Exception
                Throw New LSWFrameworkException(ex)
            End Try

            Dim web = CType(HttpWebRequest.Create(url), HttpWebRequest)
            web.Method = "POST"
            web.ContentType = "application/x-www-form-urlencoded"
            Dim bs = Encoding.UTF8.GetBytes("ChannelUrl=" & ChannelUri.Encryption(channel.Uri))
            Dim rs = Await web.GetRequestStreamAsync
            rs.Write(bs, 0, bs.Length)
            Try
                Dim res = Await web.GetResponseAsync
                Dim rr As New StreamReader(res.GetResponseStream)
                Return rr.ReadToEnd
            Catch ex As Exception
                Throw New LSWFrameworkException(ex)
            End Try
        End Function
    End Module
End Namespace